<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 * MY ROOT MODEL
 */
class MY_Model extends CI_Model
{

	public function __construct()
	{
		parent::__construct();
	}
	
	protected function total_rows()
	{
		return $this->db->query("SELECT FOUND_ROWS() AS total_rows")->row()->total_rows;
	}

	protected function my_result($limit = 0, $offset = 0)
	{
		if($limit)
			$this->db->limit($limit, $offset);

		$q = $this->db->get();

		if($q->num_rows() > 0)
		{
			$result = array(
				'last_query' => $this->db->last_query(),
				'total_rows' => $this->total_rows(),
				'rows' => $q->result()
			);
		}
		else
		{
			$result = array(
				'last_query' => $this->db->last_query(),
				'total_rows' => 0,
				'rows' => FALSE
			);
		}

		return $result;
	}

	protected function _where($options = array()) {

        if (isset($options['where'])) {
            $this->db->where($options['where']);
        }

    }

    protected function _where_in($options = array()) {

        if (isset($options['where_in'])) {
            
            $this->db->where_in($options['where_in']['column'], $options['where_in']['data']);
        }

    }

    protected function _or_where($options = array()) {

        if (isset($options['or_where'])) {

            foreach ($options['or_where'] as $key => $value) {
                // dizi içerisinde aynı anahtara sahip 2 değer bulunamayacağından $key, $value olacağına $value, $key oluyor.
                $this->db->or_where($value, $key);
            }
        }

    }

    protected function _like($options = array()) {

        if (isset($options['like'])) {
            foreach ($options['like'] as $key => $value) {
                $this->db->like($key, $value);
            }
        }

    }

    protected function _or_like($options = array()) {

        if (isset($options['or_like'])) {
            foreach ($options['or_like'] as $key => $value) {
                $this->db->or_like($key, $value);
            }
        }

    }

    protected function _limit($options = array()) {

        if (isset($options['limit'])) {
            if (isset($options['limit']['offset'])) {
                $this->db->limit($options['limit']['limit'], $options['limit']['offset']);
            } else {
                $this->db->limit($options['limit']['limit']);
            }
        }

    }
}
/* End of file MY_Model.php */
